<?php

// start the session
session_start();

// start buffering the output
ob_start();

// require the database and login libraries
require_once("database.php");
require_once("liblogin.php");

// check the $_POST variables
if(!empty($_POST['submitted']))
{
	if($_POST['submitted'] == "true")
	{
		// check the username and password after sanitizing the data
		$loggedIn = processLogin(sanitizeInput($_POST['loginUsername']), sanitizeInput($_POST['loginPassword']));
		
		if($loggedIn)
		{
			// the user successfully logged-in, so re-direct to necessary page
			print Header("Location: " . getLoggedInURL());
		}
	}
}
?>

<html>
<head>
<title>UC Irvine | Professor Assignment System - Login</title>
</head>

<body>

<?php echo createFixedMenu(); ?>

<div id="loginContainer" align="center" style="background-color:#CCCCCC;width:300px;border:dotted 1px black;margin-left:auto;margin-right:auto">

<h3>Login</h3><br />

<form name="loginForm" method="POST" action="<?php echo $_SERVER['SCRIPT_FILENAME']; ?>">
	
	<?php
	
	// the user has made an error if this clause gets executed
	if(!empty($_POST['submitted']))
	{
		echo "<span name=\"errorLabel\" style=\"color:red;\">";
		echo "Invalid username/password combination<br /><br />";
		echo "</span>";
	}
	
	?>

	<table border="0">
		<tr><td align="right">Username:</td><td align="left"><input type="text" size="20" name="loginUsername" value="<?php if(!empty($_POST['loginUsername'])) {echo $_POST['loginUsername'];} ?>" /></td></tr>
		<tr><td align="right">Password:</td><td align="left"><input type="password" size="20" name="loginPassword" value="<?php if(!empty($_POST['loginPassword'])) {echo $_POST['loginPassword'];} ?>" /></td></tr>
		<tr><td colspan="2" align="center"><br /><input type="submit" value="Login" /></td></tr>
	</table>
	<input type="hidden" name="submitted" value="true" />
</form>

</div><br />

<?php include("includes/footer.php"); ?>

</body>

</html>

<?php

// print out the buffer
ob_end_flush();

?>